package com.yanqu.road.dao.impl.union.parergon;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.parergon.UnionBussinessmanDetail;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionBussinessmanDetailDaoImpl extends TempDataObjectDao<UnionBussinessmanDetail> {

    public Map<Integer, List<UnionBussinessmanDetail>> getUnionBussinessmanDetail() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UnionBussinessmanDetail>> dataMap = new ConcurrentHashMap<>();
        String sql = "select a.* from t_u_union_bussinessman_detail a left join t_u_union_bussinessman b on a.bussinessId = b.bussinessId where b.disappearTime > ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            long now = System.currentTimeMillis() / 1000;
            int i = 1;
            call.setLong(i++, now);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionBussinessmanDetail data = parse(rs);
                List<UnionBussinessmanDetail> details = dataMap.get(data.getBussinessId());
                if (details == null) {
                    details = new ArrayList<>();
                    dataMap.put(data.getBussinessId(), details);
                }
                details.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    @Override
    protected boolean addData(UnionBussinessmanDetail data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_union_bussinessman_detail (bussinessId, userId, bargainPrice, bargainTime, hasBuy) VALUES (?, ?, ?, ?, ?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getBussinessId());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getBargainPrice());
            call.setLong(i++, data.getBargainTime());
            call.setBoolean(i++, data.isHasBuy());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UnionBussinessmanDetail data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_union_bussinessman_detail SET bargainPrice=?, bargainTime=?, hasBuy=? WHERE bussinessId=? AND userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getBargainPrice());
            call.setLong(i++, data.getBargainTime());
            call.setBoolean(i++, data.isHasBuy());
            call.setInt(i++, data.getBussinessId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UnionBussinessmanDetail parseData(ResultSet rs) throws SQLException {
        UnionBussinessmanDetail data = new UnionBussinessmanDetail();
        data.setBussinessId(rs.getInt("bussinessId"));
        data.setUserId(rs.getLong("userId"));
        data.setBargainPrice(rs.getLong("bargainPrice"));
        data.setBargainTime(rs.getLong("bargainTime"));
        data.setHasBuy(rs.getBoolean("hasBuy"));
        return data;
    }
}
